package com.v1.video.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.iss.view.common.ToastAlone;
import com.iss.view.pulltorefresh.PullToRefreshBase;
import com.iss.view.pulltorefresh.PullToRefreshBase.Mode;
import com.iss.view.pulltorefresh.PullToRefreshBase.OnRefreshListener2;
import com.iss.view.pulltorefresh.PullToRefreshListView;
import com.umeng.analytics.MobclickAgent;
import com.v1.video.Constant;
import com.v1.video.R;
import com.v1.video.adapter.MyLoginVideoAdapter;
import com.v1.video.domain.LoginInfo;
import com.v1.video.domain.MyloginOtherInfoEntity;
import com.v1.video.domain.MyloginVideoEntity;
import com.v1.video.domain.ResultInfoEntity;
import com.v1.video.domain.SetHead;
import com.v1.video.domain.UserInfo4_4;
import com.v1.video.domain.VideoInfo4;
import com.v1.video.httpApi.V1VideoHttpApi;
import com.v1.video.httpmanager.ParamList;
import com.v1.video.httpmanager.ParamList.Parameter;
import com.v1.video.httpmanager.RequestManager;
import com.v1.video.httpmanager.RequestManager.OnResponseListener;
import com.v1.video.util.Helper;
import com.v1.video.util.Logger;

import java.util.ArrayList;
import java.util.List;
/**
 * 他人个人主页
 *需要接口获取 性别，认证，视频数，粉丝数，关注关系(评论进的时候啥也没)
 */
public class OtherPersonHomeActivity extends BaseActivity implements OnClickListener {

	private final String TAG = "OtherPersonHomeActivity";
	private View mViewHead;
	private PullToRefreshListView mRefreshListView;
	private ListView mListView;
	private MyLoginVideoAdapter mAdapter;
	private int curIndex = 0;
	private List<VideoInfo4> mLstData = new ArrayList<VideoInfo4>();
	private String userId = "";
	private String userFocousType = "0";
	private ImageView imgHead;
	private ImageView imgLevel;
	private ImageView imgSex;
	private TextView txtUserName;
	private TextView txtUserDes;
	private TextView txtSubscribe;
	private TextView txtUserInfo;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_other_person_home);
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		mViewHead = getLayoutInflater().inflate(
				R.layout.header_other_person_home, null);
		
		mRefreshListView = (PullToRefreshListView) this.findViewById(R.id.theme_detail_listView);
		mRefreshListView.setScrollingWhileRefreshingEnabled(false);
		mRefreshListView.setMode(Mode.PULL_FROM_START);
		mListView = mRefreshListView.getRefreshableView();
		mListView.addHeaderView(mViewHead);
		imgHead = (ImageView)mViewHead.findViewById(R.id.mylogin_user_head_img);
		imgLevel = (ImageView)mViewHead.findViewById(R.id.iv_user_level);
		imgSex = (ImageView)mViewHead.findViewById(R.id.iv_sex);
		txtUserName = (TextView)mViewHead.findViewById(R.id.mylogin_user_nickname);
		txtUserDes = (TextView)mViewHead.findViewById(R.id.header_other_des_info);
		txtSubscribe = (TextView)mViewHead.findViewById(R.id.header_other_subscribe_btn);
		txtUserInfo = (TextView)mViewHead.findViewById(R.id.mylogin_user_description);
	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		Intent intent = this.getIntent();
		userId = intent.getStringExtra("userId");
		String userName = intent.getStringExtra("nickName");
		String userSex = intent.getStringExtra("userSex");
		String userIsV = intent.getStringExtra("isCertification");
		String userHead = intent.getStringExtra("userHead");
		String userDesc = intent.getStringExtra("userDesc");
		userFocousType = intent.getStringExtra("focousType");
		txtUserName.setText(userName);
		setUserInfo(userSex,userIsV,userDesc,"0","0");
		SetHead setHead = new SetHead();
		setHead.setHead(userHead, userSex, imgHead);
		getUserInfo();
		mAdapter = new MyLoginVideoAdapter(this,null,0);
		mListView.setAdapter(mAdapter);
		if (mRefreshListView != null) {
			mRefreshListView.doPullRefreshing(100);
		}		
	}
	
	private void setUserInfo(String userSex,String userIsV,String userDesc,String videoNum,String fansNum){
		imgSex.setVisibility(View.VISIBLE);
//		if(userSex.equals("女")){
//			imgSex.setImageResource(R.drawable.icon_sex_woman);
//		}
		if(userIsV.equals("1")){
			imgLevel.setVisibility(View.VISIBLE);
		}else{
			imgLevel.setVisibility(View.GONE);
		}
		txtUserDes.setText("简介："+userDesc);
		if(userFocousType.equals("1")){
			txtSubscribe.setText("已订阅");
		}else if(userFocousType.equals("2")){
			txtSubscribe.setText("相互订阅");
		}else {
			txtSubscribe.setText("+订阅");
		}
		txtUserInfo.setText("视频："+videoNum + "  |  "+"粉丝："+fansNum);
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPageEnd("他人信息页");
		MobclickAgent.onPause(this);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MobclickAgent.onPageStart("他人信息页");
		MobclickAgent.onResume(this);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	protected void setListener() {
		// TODO Auto-generated method stub
		this.findViewById(R.id.lay_result).setVisibility(View.VISIBLE);
		this.findViewById(R.id.lay_result).setOnClickListener(this);
		txtSubscribe.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (Constant.ATTENTION_TYPE_NOT.equals(userFocousType)
						|| Constant.ATTENTION_TYPE_BY.equals(userFocousType)) {
					getAddOrCancelFocous(userId,Constant.ATTENTION_TYPE_ADD);
				} else if (Constant.ATTENTION_TYPE_YET.equals(userFocousType)
						|| Constant.ATTENTION_TYPE_TOGEHER.equals(userFocousType)) {
					getAddOrCancelFocous(userId,Constant.ATTENTION_TYPE_CANCEL);
				}else{
					getAddOrCancelFocous(userId,Constant.ATTENTION_TYPE_ADD);
				}
			}
		});
		mRefreshListView.setOnRefreshListener(new OnRefreshListener2() {

			@Override
			public void onPullDownToRefresh(PullToRefreshBase refreshView) {
				// TODO Auto-generated method stub
				if (Helper.checkConnection(OtherPersonHomeActivity.this)) {
					curIndex = 0;
					getServerData();
				} else {
					if (mRefreshListView != null) {
						mRefreshListView.onRefreshComplete();
					}
					Toast.makeText(OtherPersonHomeActivity.this,
							getResources().getString(R.string.net_no_notclick),
							Toast.LENGTH_SHORT).show();
				}
			}

			@Override
			public void onPullUpToRefresh(PullToRefreshBase refreshView) {
				// TODO Auto-generated method stub
				if (Helper.checkConnection(OtherPersonHomeActivity.this)) {
					curIndex++;
					getServerData();
				} else {
					if (mRefreshListView != null) {
						mRefreshListView.onRefreshComplete();
					}
					Toast.makeText(OtherPersonHomeActivity.this,
							getResources().getString(R.string.net_no_notclick),
							Toast.LENGTH_SHORT).show();
				}
			}
		});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.lay_result:
			this.finish();
			break;
		default:
			break;
		}
	}
	
	private void getUserInfo(){
		String url = V1VideoHttpApi.GET_USER_INFO_OF_OTHER() + "&uid="+userId+"&devid"+Constant.DEVICEID+"&muid="+LoginInfo.getInstance().getUserId();
    	Logger.i(TAG, "获取他人信息页Url ="+url);
		RequestManager.getInstance().getRequest(OtherPersonHomeActivity.this, url, null, MyloginOtherInfoEntity.class, 0, "OtherPersonHomeActivity", new OnResponseListener(){

			@Override
			public void onSuccess(Object object, int code, String message,String key) {
				// TODO Auto-generated method stub
				Logger.i(TAG, "onSuccess.object="+object.toString());
				
				if(object != null){
					MyloginOtherInfoEntity entity = (MyloginOtherInfoEntity)object;
					com.v1.video.domain.BaseEntity.Header header = entity.getHeader();
					if(header == null){
						ToastAlone.showToast(OtherPersonHomeActivity.this, "未知错误", Toast.LENGTH_SHORT);
						return;
					}else if(!header.getStatus().equals("1")){
						ToastAlone.showToast(OtherPersonHomeActivity.this, header.getMessage(), Toast.LENGTH_SHORT);
						return;
					}else if(header.getStatus().equals("1")){
						if(entity.getBody() != null){
							if(entity.getBody().getData() != null){
								if(entity.getBody().getStatus().equals("1")){
									UserInfo4_4 user = entity.getBody().getData();
									userFocousType = user.getType();
									setUserInfo(user.getGender(),user.getIsauth(),user.getSignature(),user.getVideo_num(),user.getFans_num());
								}
							}else{
								
							}
							
						}else{
							toast(header.getMessage());
						}
					}
				}
				if(mRefreshListView != null){
					mRefreshListView.onRefreshComplete();
				}
			}

			@Override
			public void onFailure(int code, String message) {
				// TODO Auto-generated method stub
				Logger.i(TAG, message);
//				if(curIndex == 0){
//					mListView.setEmptyView(mEmptyView);
//				}
				if(mRefreshListView != null){
					mRefreshListView.onRefreshComplete();
				}
			}
		});
	}

	/**
	 * 获取服务器数据
	 */
	private void getServerData() {
		String url = V1VideoHttpApi.GET_VIDEO_LIST_USER() + "&uid="+userId+"&p="+curIndex+"&s=20"+"&devid="+Constant.DEVICEID;
    	Logger.i(TAG, "视频列表Url ="+url);
		RequestManager.getInstance().getRequest(OtherPersonHomeActivity.this, url, null, MyloginVideoEntity.class, 0, "OtherPersonHomeActivity", new OnResponseListener(){

			@Override
			public void onSuccess(Object object, int code, String message,String key) {
				// TODO Auto-generated method stub
				Logger.i(TAG, "onSuccess.object="+object.toString());
				if(mRefreshListView != null){
					mRefreshListView.onRefreshComplete();
				}
				if(object != null){
					MyloginVideoEntity entity = (MyloginVideoEntity)object;
					com.v1.video.domain.BaseEntity.Header header = entity.getHeader();
					if(header == null){
						ToastAlone.showToast(OtherPersonHomeActivity.this, "未知错误", Toast.LENGTH_SHORT);
						return;
					}else if(!header.getStatus().equals("1")){
						ToastAlone.showToast(OtherPersonHomeActivity.this, header.getMessage(), Toast.LENGTH_SHORT);
						return;
					}else if(header.getStatus().equals("1")){
						if(entity.getBody() != null){
							if(entity.getBody().getData() != null){
								if(mLstData != null){
									mLstData.addAll(entity.getBody().getData());
								}else{
									mLstData = entity.getBody().getData();
								}
								if(curIndex == 0){
									mAdapter.setLstData(mLstData);
								}else{
									mAdapter.setAddLstData(mLstData);
								}
								mAdapter.notifyDataSetChanged();
							}else{
//								if(curIndex == 0){
//									mListView.setEmptyView(mEmptyView);
//								}
							}
							
						}else{
//							toast(header.getMessage());
						}
					}
				}
				
			}

			@Override
			public void onFailure(int code, String message) {
				// TODO Auto-generated method stub
				Logger.i(TAG, message);
//				if(curIndex == 0){
//					mListView.setEmptyView(mEmptyView);
//				}
				if(mRefreshListView != null){
					mRefreshListView.onRefreshComplete();
				}
			}
		});
	}
	
	/**加关注或取消关注*/
    private void getAddOrCancelFocous(String focusUserId, final String typeId){
    	String url = V1VideoHttpApi.GET_VIDEO_TO_FOCOUS_ABOUT() +"&devid="+Constant.DEVICEID;
    	ParamList list = new ParamList();
    	list.add(new Parameter("fuid", focusUserId));
    	list.add(new Parameter("type", typeId));
    	list.add(new Parameter("uid", LoginInfo.getInstance().getUserId()));
		RequestManager.getInstance().getRequest(OtherPersonHomeActivity.this, 1,url, list, ResultInfoEntity.class, 0, "OtherPersonHomeActivity", new OnResponseListener(){

			@Override
			public void onSuccess(Object object, int code, String message,String key) {
				// TODO Auto-generated method stub
				Logger.i(TAG, "onSuccess.object="+object.toString());
				
				if(object != null){
					ResultInfoEntity entity = (ResultInfoEntity)object;
					com.v1.video.domain.BaseEntity.Header header = entity.getHeader();
					if(header == null){
						ToastAlone.showToast(OtherPersonHomeActivity.this, "未知错误", 1);
						return;
					}else if(!header.getStatus().equals("1")){
						ToastAlone.showToast(OtherPersonHomeActivity.this, header.getMessage(), 1);
						return;
					}else if(header.getStatus().equals("1")){
						if(entity.getBody() != null){
							if(entity.getBody().getResult().getCode().equals(Constant.RESULT_CODE_SUCCESS)){
								if (Constant.ATTENTION_TYPE_ADD.equals(typeId)) {
									// 加关注成功，变成已经关注
									if (Constant.ATTENTION_TYPE_NOT.equals(userFocousType)) {
										txtSubscribe.setText("已订阅");
									} else if (Constant.ATTENTION_TYPE_BY
											.equals(userFocousType)) {
										txtSubscribe.setText("相互订阅");
									}
								} else if (Constant.ATTENTION_TYPE_CANCEL.equals(typeId)) {
									// 取消关注成功，变成加关注
									if (Constant.ATTENTION_TYPE_YET.equals(userFocousType)) {
									} else if (Constant.ATTENTION_TYPE_TOGEHER
											.equals(userFocousType)) {
									}
									txtSubscribe.setText("+订阅");
								}
							}
						}else{
//							toast(header.getMessage());
						}
					}
				}
			}

			@Override
			public void onFailure(int code, String message) {
				// TODO Auto-generated method stub
				Logger.i(TAG, message);
			}
		});
    }
}
